package com.helen.ui.order;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.helen.db.UserHelper;
import com.helen.global.Constants;
import com.helen.shopping.BuildConfig;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.ui.account.RefreshMoneyUtils;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.RxPayUtils;
import com.helen.utils.tiputils.MToast;
import com.weilu.pay.annotation.WXPay;
import com.weilu.pay.api.RxWxPay;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/5/28.
 * 确认支付
 */
@WXPay(BuildConfig.APPLICATION_ID)
public class PaidOrderActivity extends BaseActivity {
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_use_yue)
    TextView tvUseYue;
    @BindView(R.id.cb_balance)
    CheckBox cbBalance;
    @BindView(R.id.cb_zhifubao)
    CheckBox cbZhifubao;
    @BindView(R.id.cb_weixin)
    CheckBox cbWeixin;
    @BindView(R.id.tv_sure_paid)
    SuperTextView tvSurePaid;
    private String allPrice;
    private String pay_method = "1";//1、支付宝 2、微信 3、余额
    private String orderid;
    private String s_orderid;
    private int fromType = 1;//1 订单列表或者详情付款，2购物车或者商品详情购物

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_sure_paid;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        orderid = getIntent().getStringExtra("orderid");
        s_orderid = getIntent().getStringExtra("s_orderid");
        fromType = getIntent().getIntExtra("fromType", 1);
        getToolbarTitle().setText(R.string.battalion_station);
        allPrice = getIntent().getStringExtra("allPrice");
        tvAmount.setText(allPrice + "");


        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (fromType) {
                    case 1:
                        finish();
                        break;
                    case 2:
                        Intent ti = new Intent(PaidOrderActivity.this, OrderActivity.class);
                        ti.putExtra("currentId", 1);
                        startActivity(ti);
                        finish();
                        break;
                }

            }
        });


        RefreshMoneyUtils refreshMoneyUtils = new RefreshMoneyUtils();
        refreshMoneyUtils.refreshMoneyApi(PaidOrderActivity.this);
        refreshMoneyUtils.setOnResultListener(new RefreshMoneyUtils.OnResultListener() {
            @Override
            public void onResult(double money, int jinbao) {
                tvUseYue.setText(money + "元");
                if (money == 0 || money < Double.parseDouble(allPrice)) {
                    cbBalance.setClickable(false);
                } else {
                    cbBalance.setClickable(true);
                }
            }
        });
    }

    @Override
    protected void updateViews() {

    }


    @OnClick({R.id.cb_balance, R.id.cb_zhifubao, R.id.cb_weixin, R.id.tv_sure_paid})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cb_balance:
                pay_method = "3";
                cbBalance.setChecked(true);
                cbZhifubao.setChecked(false);
                cbWeixin.setChecked(false);
                break;
            case R.id.cb_zhifubao:
                pay_method = "1";
                cbZhifubao.setChecked(true);
                cbWeixin.setChecked(false);
                cbBalance.setChecked(false);
                break;
            case R.id.cb_weixin:
                pay_method = "2";
                cbWeixin.setChecked(true);
                cbZhifubao.setChecked(false);
                cbBalance.setChecked(false);
                break;
            case R.id.tv_sure_paid:
                switch (pay_method) {
                    case "1":
                    case "2":
                    case "3":
                        payOrderApi();
                        break;
                }
                break;
        }
    }

    private void payOrderApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("userid", UserHelper.getInstance().getloginEntity(PaidOrderActivity.this).getId() + "");
        if (!TextUtils.isEmpty(orderid)) {
            params.put("orderid", orderid);
        }
        if (!TextUtils.isEmpty(s_orderid)) {
            params.put("s_orderid", s_orderid);
        }
        params.put("money", allPrice);
        params.put("pay_method", pay_method);
        MyLog.e("yang", "params==" + params.toString());
        EasyHttp.post(Constants.API_JSONAPI_PAYORDER)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "付款失败==" + e.toString());
                        MToast.makeTextLong(PaidOrderActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "付款成功==" + result);
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            String mResult = JsonUtil.getJsonValuesString(result, "result");
                            if (pay_method.equals("1")) {
                                RxPayUtils.aLiPay(PaidOrderActivity.this, mResult);
                            } else if (pay_method.equals("2")) {
                                RxPayUtils.wechatPay(PaidOrderActivity.this, mResult);
                            } else if (pay_method.equals("3")) {
                                Intent ti = new Intent(PaidOrderActivity.this, OrderActivity.class);
                                ti.putExtra("currentId", 2);
                                PaidOrderActivity.this.startActivity(ti);
                                finish();
                            }
                        } else {
                            MToast.makeTextShort(PaidOrderActivity.this, msg).show();
                        }
                    }
                });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) {
            switch (fromType) {
                case 1:
                    finish();
                    break;
                case 2:
                    Intent ti = new Intent(PaidOrderActivity.this, OrderActivity.class);
                    ti.putExtra("currentId", 1);
                    startActivity(ti);
                    finish();
                    break;
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxWxPay.getInstance().onDestroy();
    }


}
